package com.ssy.lingxi.after.sale.repository;

import com.ssy.lingxi.after.sale.entity.ReturnGoodsRefundDetailDO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售后退货退款明细
 * @author 伍将
 * @date 2020/9/25
 * @version 2.0.0
 */
public interface ReturnGoodsRefundDetailRepository extends JpaRepository<ReturnGoodsRefundDetailDO, Long>, JpaSpecificationExecutor<ReturnGoodsRefundDetailDO> {

    /**
     * 查询退货未退款记录
     * @author 伍将
     * @date 2021/1/29
     * @param returnId: 退货id
     * @return true-是，false-否
     **/
    @Query(value = "select count(id) from as_return_goods_refund_detail " +
            "where return_id =:returnId " +
            "and external_state=3 " +
            "and inner_status not in(3,4)", nativeQuery = true)
    Long getReturnUnRefund(Long returnId);

    /**
     * 查询退货未完成退款记录
     * @author 伍将
     * @date 2021/1/29
     * @param returnId: 退货id
     * @return true-是，false-否
     **/
    @Query(value = "select count(id) from as_return_goods_refund_detail " +
            "where return_id =:returnId " +
            "and outer_status not in(4,5) ", nativeQuery = true)
    Long getReturnUnCompleteRefund(Long returnId);

    /**
     * 查询订单未退款数量
     * @author 伍将
     * @date 2021/1/28
     * @param orderId: 订单id
     * @return 未退款数量
     **/
    @Query(value = "select count(d.id) from as_return_goods_refund_detail d " +
            "inner join as_return_goods r " +
            "on d.return_id=r.id " +
            "where d.order_id =:orderId " +
            "and r.outer_status not in(3,14,15)", nativeQuery = true)
    Long getOrderUnRefundCount(Long orderId);

    /**
     * 删除退款记录
     * @author 伍将
     * @date 2021/1/28
     * @param returnId: 售后退货id
     * @return 无
     **/
    @Transactional
    void deleteAllByReturnId(Long returnId);
}
